package com.nl.lib_project.net

import com.hjq.http.EasyUtils
import com.hjq.http.exception.ResultException
import com.hjq.http.listener.OnHttpListener
import com.nl.lib_core.mvp.IBaseView
import com.nl.lib_project.datas.LocalDbAutism
import okhttp3.Call

/**
 * 作者: wangfei 创建时间: 2022/1/21 包名: com.nl.lib_project.net 类名: NLAppHttpCallback
 * 当前统一处理网络请求成功后是否需要根据错误码进行展示页面或者失败后页面展示逻辑 使用在当前需要展示不同View的容器当中
 */
open class NLAppHttpCallback<T : ResponseBaseBean<*>>(
    private val mView: IBaseView?,
    private var isShowDialog: Boolean = true // 默认显示
) : OnHttpListener<T> {

  // 600 表示当前没有授权的设备code码
  private var mCode = 0

  override fun onStart(call: Call?) {
    super.onStart(call)
    if (!isShowDialog) {
      return
    }
    mView?.showInitLoadView()
    mView?.hideNetWorkErrView()
    mView?.hideAuthorView()
    mView?.hideNoDataView()
    mView?.hideAppErrorView()
  }

  override fun onSucceed(result: T) {
    mCode = result.code
  }

  override fun onFail(e: Exception?) {
    if (!isShowDialog) {
      return
    }
    if (e is ResultException) {
      val code = (e.data as ResponseBaseBean<*>).code
      if (code == LocalDbAutism.DEVICE_ID_ILLEGAL || code == LocalDbAutism.DEVICE_ID_UNCERTIFIED) {
        mCode = code
        mView?.showAuthorView()
      } else {
        mView?.showNetWorkErrView()
      }
    } else {
      mView?.showNetWorkErrView()
    }
  }

  override fun onEnd(call: Call?) {
    super.onEnd(call)
    if (!isShowDialog) {
      return
    }
    EasyUtils.postDelayed({ mView?.hideInitLoadView() }, LocalDbAutism.VIEW_SCROLL_DURATION_500L)
  }
}
